package com.github.thinwonton.spring.web.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.github.thinwonton.spring.datasource.UserDataSource;
import com.github.thinwonton.spring.entity.RestfulErrorResponse;
import com.github.thinwonton.spring.entity.User;

@Controller
@RequestMapping("/user")
public class UserController {

	/**
	 * 查询用户列表
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public ResponseEntity<?> queryUserList() {
		try {
			List<User> list = UserDataSource.getUserList();
			if (list == null || list.size() <= 0) {
				// 资源找不到
				// 404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RestfulErrorResponse("请求的资源找不到"));
			}
			return ResponseEntity.ok(list);
		} catch (Exception e) {
			e.printStackTrace();
			// 记录日志
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}

	}

	/**
	 * 根据用户ID查询用户
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{userid}", method = RequestMethod.GET, produces = { "application/json;charset=UTF-8" })
	public ResponseEntity<?> queryUserById(@PathVariable(value = "userid") Long id) {
		System.out.println("GET");
		try {
			if (id <= 0) {
				// 参数非法
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestfulErrorResponse("请求参数非法"));
			}

			// 查询用户
			User user = UserDataSource.getById(id);
			if (user == null) {
				// 资源找不到
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RestfulErrorResponse("请求的资源找不到"));
			}
			return ResponseEntity.ok(user);
		} catch (Exception e) {
			e.printStackTrace();
			// 记录日志
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}

	}

	/**
	 * 保存用户
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.POST, produces = { "application/json;charset=UTF-8" })
	public ResponseEntity<?> saveUser(User user) {
		System.out.println("POST : "+user.toString());
		try {
			// 保存用户
			UserDataSource.save(user);
			// 响应201
			return ResponseEntity.status(HttpStatus.CREATED).build();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO 记录日志
			// 500
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}

	/**
	 * 更新用户
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.PUT, produces = { "application/json;charset=UTF-8" })
	public ResponseEntity<?> updateUser(User user) {
		try {
			// 保存用户
			int count = UserDataSource.update(user);
			if (count > 0) {
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			} else {
				// 响应404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RestfulErrorResponse("请求的资源找不到"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO 记录日志
			// 500
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}

	/**
	 * 删除用户
	 * 
	 * @return
	 */
	@RequestMapping(method = RequestMethod.DELETE, produces = { "application/json;charset=UTF-8" })
	public ResponseEntity<?> deleteById(@RequestParam(value = "id", defaultValue = "0") Long id) {
		System.out.println("delete ---> ID=" + id);
		try {
			if (id <= 0) {
				// 参数非法 400
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new RestfulErrorResponse("请求参数非法"));
			}

			int count = UserDataSource.delete(id);
			if (count > 0) {
				// 204
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
			} else {
				// 响应404
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new RestfulErrorResponse("请求的资源找不到"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			// TODO 记录日志
			// 500
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
}
